import json

from social.call.models import AVCall
from social.im.api import ZIMAPI
from social.utils.celery_app import app


@app.task()
def send_call_over_message(call_id):
    call = AVCall.objects.select_related('from_user', 'to_user').get(id=call_id)
    message = {
        'duration': call.duration,
        'status': call.status,
        'type': call.type
    }
    ZIMAPI().send_custom_message(from_user_id=call.from_user.im_id, to_user_id=call.to_user.im_id,
                                 sub_msg_type=2, message=str(json.dumps(message)))
